package com.wanmait.spacesys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.x.protobuf.MysqlxCrud.Update;
import com.wanmait.spacesys.dao.ConsultingArticleDAO;
import com.wanmait.spacesys.vo.Article;
import com.wanmait.spacesys.vo.PhotoNew;

/**
 * Servlet implementation class ConsultingArticleController
 */
@WebServlet("/ConsultingArticleController")
public class ConsultingArticleController extends BaseController {
	// 按照资讯区官方文章发布的事件查询照片 ajax
	protected void consulting(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
		List<Article> articles = ConsultingArticleDAO.getConsultingArticleDAO().findAllArticle();
//		System.out.println(articles.get(0).getPubTime());
		JSONArray.writeJSONString(response.getWriter(), articles);

	}

	// 按照最新时间显示首页精彩图片 ajax
	protected void findAllPhoto(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
		List<PhotoNew> photoNews = ConsultingArticleDAO.getConsultingArticleDAO().findAllPhoto();
		/* photoNews.forEach(photoNew -> System.out.println(photoNew.getPic())); */
		JSONArray.writeJSONString(response.getWriter(), photoNews);
	}

	// 资讯区官方文章的id查询资讯区官方文章的详细信息
	protected void findById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String ids = request.getParameter("id");
		/* System.out.println(ids); */
		Integer id = Integer.parseInt(ids);
        List<Article> articleTitles=ConsultingArticleDAO.getConsultingArticleDAO().findAllArticle();
		/* System.out.println(articleTitles); */
        List<Article> articlePubTimes=ConsultingArticleDAO.getConsultingArticleDAO().findAllArticle();
		Article article = ConsultingArticleDAO.getConsultingArticleDAO().findById(id);
		/* article.setId(article.getId()+1); */
        request.setAttribute("articleTitles", articleTitles);
        request.setAttribute("articlePubTimes", articlePubTimes);
		request.setAttribute("article", article);
		this.forward("/web/user/contArticle.jsp", request, response);

	}

	//资讯区官方文章的id查询资讯区官方文章的发布人文章详细信息
	protected void findManagerById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String ids = request.getParameter("id");
		Integer id = Integer.parseInt(ids);

		Article article = ConsultingArticleDAO.getConsultingArticleDAO().findManagerById(id);
		request.setAttribute("article", article);
		this.forward("/web/user/contManager.jsp", request, response);

	}

	// Ajax //改变监听器的数字
	protected void getOnline(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获得application对象
		ServletContext application = request.getServletContext();
		int count = (int) application.getAttribute("onlineCount");
		/* System.out.println(count); */
		// 写
		PrintWriter out = response.getWriter();
		// count变成字符串
		out.print(count + "");
		
	}

	@Override
	protected void doDefault(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.consulting(request, response);

	}

}
